package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 23.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 我是宝爸
 */
public class IAmTreasureDedActivity extends Activity {
    private static final String TAG = "IAmTreasureDedActivity";
    @Bind(R.id.activity_i_am_treasure_ded_iv_return)
    ImageView activityIAmTreasureDedIvReturn;
    @Bind(R.id.activity_i_am_treasure_ded_tv_code)
    TextView activityIAmTreasureDedTvCode;
    @Bind(R.id.activity_i_am_treasure_ded_et_input_code)
    EditText activityIAmTreasureDedEtInputCode;
    @Bind(R.id.activity_i_am_treasure_ded_tv_submit)
    TextView activityIAmTreasureDedTvSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_i_am_treasure_ded);
        ButterKnife.bind(this);

        activityIAmTreasureDedEtInputCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                activityIAmTreasureDedTvCode.setText("");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() < 6){
                    activityIAmTreasureDedTvCode.setText(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.activity_i_am_treasure_ded_iv_return, R.id.activity_i_am_treasure_ded_tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_i_am_treasure_ded_iv_return:
                finish();
                break;
            case R.id.activity_i_am_treasure_ded_tv_submit:
                String InputCode = activityIAmTreasureDedTvCode.getText().toString().trim();
                if(!TextUtils.isEmpty(InputCode)){
                    submitInvitationCodeDate(InputCode);
                }else{
                    Toast.makeText(IAmTreasureDedActivity.this,"请输入邀请码",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void submitInvitationCodeDate(String inputCode) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.FATHER_ENTERED_INVITAION_CODE)
                .addParams("username", "hyman")
                .addParams("password", "123")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                         String parseNetworkResponse =  response.body().string();
                         Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                         Gson gson =  new Gson();
                        // gson.fromJson(parseNetworkResponse,);


                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
